package interview;

import java.util.*;

public class lc394 {
    public static void main(String[] args) {
        System.out.println(decodeString("3[z]2[2[y]pq4[2[jk]e1[f]]]ef"));
        Map<Character, Integer> map = new HashMap<>();
        for(Map.Entry<Character, Integer> kv : map.entrySet()){

        }
    }
    public static String decodeString(String s) {
        Deque<String> stack = new ArrayDeque<>();
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == ']'){
                List<String> list = new ArrayList<>();
                while(!stack.isEmpty()){
                    String ch1 = stack.pop();
                    if(ch1.equals("[")){
                        break;
                    }
                    else{
                        list.add(ch1);
                    }
                }
                StringBuilder sb = new StringBuilder();
                for (int j = list.size() - 1; j >= 0; j--) {
                    sb.append(list.get(j));
                }
                int repeat = 0;
                int k = 0;
                while(!stack.isEmpty()){
                    String ch1 = stack.peek();
                    if(ch1.charAt(0) <= '9' && ch1.charAt(0) >= '0'){
                        stack.pop();
                        repeat = repeat + (int) ((ch1.charAt(0) - '0') * Math.pow(10, k++));
                    }
                    else break;
                }
                String str = sb.toString();
                for (int j = 0; j < repeat - 1; j++) {
                    sb.append(str);
                }
                stack.push(sb.toString());
            }
            else {
                stack.push(s.substring(i, i + 1));
            }
        }
        StringBuilder ans = new StringBuilder();
        while(!stack.isEmpty()){
            ans.append(stack.getLast());
            stack.removeLast();
        }
        return ans.toString();
    }


    /**
     * 方法二
     * @param s
     * @return
     */
    public static String decodeString2(String s) {
        Deque<String> strStack = new ArrayDeque<>();
        Deque<Integer> numStack = new ArrayDeque<>();
        StringBuilder ans = new StringBuilder();
        char[] ch = s.toCharArray();
        for (int i = 0; i < s.length(); i++) {
            StringBuilder dig = new StringBuilder();
            while(i < s.length() && ch[i] <= '9' && ch[i] >= '0'){
                dig.append(ch[i]);
                i++;
            }
            if(dig.length() != 0){
                numStack.push(Integer.parseInt(dig.toString()));
            }
            if(ch[i] == '[')
                strStack.push(ch[i] + "");
            StringBuilder str = new StringBuilder();
            while(i < s.length() && ch[i] <= 'z' && ch[i] >= 'a'){
                str.append(ch[i]);
                i++;
            }
            if(str.length() != 0){
                strStack.push(str.toString());
            }
            if(ch[i] == ']'){

            }

        }
        return ans.toString();

    }
}
/*
"3[z]2[2[y]pq4[2[jk]e1[f]]]ef"

"zzzyypqjkjkefjkjkefjkjkefjkjkefyypqjkjkefjkjkefjkjkefjkjkefef"
 */